package org.jeecg.modules.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.TigDevice;
import org.jeecg.modules.service.DeviceService;
import org.jeecg.modules.service.ITigerDeviceLogService;
import org.jeecg.modules.tigstatics.entity.TigStatistics;
import org.jeecg.modules.tigstatics.service.ITigStatisticsService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.List;


/**
 * @Description: 统计定时任务
 *
 *
 * @author: jiesheng
 * @date: 2022年09月18日
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class StaticsAutoJob implements Job {

    @Autowired
    DeviceService deviceService;

    @Autowired
    ITigStatisticsService staticsService;

    @Autowired
    ISysBaseAPI sysBaseAPI;

    @Autowired
    ITigerDeviceLogService logService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" --- StaticsAutoJob 统计任务调度开始 --- ");
        try {
            //遍历所有机构进行统计
            List<SysDepartModel> departList = sysBaseAPI.getAllSysDepart();
            for (SysDepartModel dep : departList) {
                TigStatistics statics = new TigStatistics();
                String orgCode = dep.getOrgCode();
                int onlineCount = deviceService.getOnlineDevicesCount(orgCode);
                int offlineCount = deviceService.getOfflineDevicesCount(orgCode);
                int deviceCount = deviceService.getDevicesCount(orgCode);
                int warningCount = deviceService.getWarningCount(orgCode,-1, LocalDate.now());
                int cityVolCount = deviceService.getWarningCount(orgCode,1, LocalDate.now());
                int mVolCount = deviceService.getWarningCount(orgCode,2, LocalDate.now());
                int bVolCount = deviceService.getWarningCount(orgCode,3, LocalDate.now());
                int curCount = deviceService.getWarningCount(orgCode,4, LocalDate.now());
                int upsCount = deviceService.getWarningCount(orgCode,5, LocalDate.now());
                int tempCount = deviceService.getWarningCount(orgCode,6, LocalDate.now());
                int channelCount = deviceService.getWarningCount(orgCode,7, LocalDate.now());
                statics.setDevOnlineCount(onlineCount);
                statics.setDevOfflineCount(offlineCount);
                statics.setDeviceCount(deviceCount);
                statics.setDevWarningCount(warningCount);
                statics.setCityvolWarningCount(cityVolCount);
                statics.setMainvolWarningCount(mVolCount);
                statics.setBackvolWarningCount(bVolCount);
                statics.setCurrentWarningCount(curCount);
                statics.setUpsWarningCount(upsCount);
                statics.setTempWarningCount(tempCount);
                statics.setOtherWarningCount(channelCount);
                statics.setDevExcProcCount(0);
                statics.setOrgCode(orgCode);
                staticsService.save(statics);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //log.info(" --- 执行完毕，时间："+ DateUtils.now()+"---");
    }

}
